﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.IO;
using Gift.WebApp;
using Gift.Model;
using System.Data;
using System.Text;
using System.Xml;

namespace WebSite.admin.order
{
    public partial class orderlist : BaseAdmin
    {



        protected string ds = "";
        protected string ed = "";

        protected string kw = "";
        protected string order = "id";
        protected string os = "";
        protected int ot = 1;
        protected int pageCount = 0;
        protected int pageIndex = 1;
        protected int pageSize = 20;
        protected PageList pl = new PageList();
        protected string pss = "";
        protected string sd = "";
        protected int t = 0;
        protected int total = 0;

 
        protected void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                string item =  GetSqlWhere();
                ExcelHelper.ExportDataTableToExcel(BaseFactory.theOrder.GetExportOrderByTerm(item), DateTime.Now.ToString("yyyyMMddHHmmssffffff") + ".xls", "订单列表");
            }
            catch
            {
            }
        }

        protected string GetDeliveryStatus(object deliveryStatus)
        {
            try
            {
                switch (Convert.ToInt32(deliveryStatus))
                {
                    case 0:
                        return "未发货";

                    case 1:
                        return "已发货";

                    case 2:
                        return "已收货";
                }
            }
            catch
            {
            }
            return "";
        }

        protected string GetOrderStatus(object orderStatus)
        {
            try
            {
                switch (Convert.ToInt32(orderStatus))
                {
                    case 0:
                        return "未确认";

                    case 1:
                        return "已确认";

                    case 2:
                        return "已取消";

                    case 3:
                        return "无效";

                    case 4:
                        return "退货";
                }
            }
            catch
            {
            }
            return "";
        }



        private void OrderBind()
        {
            try
            {
                if (base.Request.QueryString["p"] != null)
                {
                    this.pageIndex = int.Parse(base.Request.QueryString["p"]);
                }
                if (base.Request.QueryString["ps"] != null)
                {
                    this.pageSize = Convert.ToInt32(base.Request.QueryString["ps"]);
                }
                if (base.Request.QueryString["or"] != null)
                {
                    this.order = base.Request.QueryString["or"];
                }
                if (base.Request.QueryString["ot"] != null)
                {
                    this.ot = Convert.ToInt32(base.Request.QueryString["ot"]);
                }
                this.pl.TblName = "view_order";
                this.pl.StrGetFields = "id,orderNo,orderUser,userName,orderStatus,deliveryStatus,payStatus,Convert(nvarchar(10),createTime,121) as createTime,orderTotal,deliveryWay,payment ";
                this.pl.FldName = this.order;
                this.pl.PageSize = this.pageSize;
                this.pl.PageIndex = this.pageIndex;
                this.pl.DoCount = 1;
                this.pl.OrderType = this.ot;
                this.pl.StrWhere =  GetSqlWhere();
               

                this.total = Convert.ToInt32(BaseFactory.thePageList.GetPageList(this.pl).Rows[0]["Total"]);
                this.pl.DoCount = 0;
                DataTable pageList = BaseFactory.thePageList.GetPageList(this.pl);
                new PageListNo().DelPageSize(pageList, this.total);
                this.rptOrder.DataSource = pageList.DefaultView;
                this.rptOrder.DataBind();
            }
            catch
            {
            }
        }

        string GetSqlWhere()
        {
            StringBuilder sbWhere = new StringBuilder("1=1 ");
            if ((base.Request.QueryString["t"] != null) && (base.Request.QueryString["kw"] != null))
            {
                this.kw = base.Request.QueryString["kw"].Trim();
              sbWhere.AppendFormat(" and {0} like '%{1}%'"
                                  , base.Request.QueryString["t"]
                                  , this.kw);
            }
            if ((base.Request.QueryString["sd"] != null) && (base.Request.QueryString["ed"] != null))
            {
                this.sd = base.Request.QueryString["sd"];
                this.ed = base.Request.QueryString["ed"];
                sbWhere.AppendFormat(" and createTime>={0} 00:00:00' and createTime<='{1} 23:59:59'"
                                     , this.sd
                                     , this.ed);
            }
            if (base.Request.QueryString["os"] != null)
            {
                this.os = base.Request.QueryString["os"];
                sbWhere.AppendFormat(" and orderStatus={0}", this.os);
            }
            if (base.Request.QueryString["ds"] != null)
            {
                this.ds = base.Request.QueryString["ds"];
                sbWhere.AppendFormat(" and deliveryStatus={0}" , this.ds);
            }
            if (base.Request.QueryString["pss"] != null)
            {
                this.pss = base.Request.QueryString["pss"];
                sbWhere.AppendFormat(" and payStatus={0}", this.pss);
            }
            return sbWhere.ToString();
        }

        protected string OrderSumTotal()
        {
            decimal num = 0M;
            decimal num2 = 0M;
            decimal sumCostPriceByTerm = 0M;
            string term = "";
            try
            {
                if ((base.Request.QueryString["t"] != null) && (base.Request.QueryString["kw"] != null))
                {
                    this.kw = base.Request.QueryString["kw"].Trim();
                    term = term + ((term != "") ? (" and " + base.Request.QueryString["t"] + " like '%" + this.kw + "%'") : (base.Request.QueryString["t"] + " like '%" + this.kw + "%'"));
                }
                if ((base.Request.QueryString["sd"] != null) && (base.Request.QueryString["ed"] != null))
                {
                    this.sd = base.Request.QueryString["sd"];
                    this.ed = base.Request.QueryString["ed"];
                    term = term + ((term != "") ? (" and createTime>='" + this.sd + " 00:00:00' and createTime<='" + this.ed + " 23:59:59'") : ("createTime>='" + this.sd + " 00:00:00' and createTime<='" + this.ed + " 23:59:59'"));
                }
                if (base.Request.QueryString["os"] != null)
                {
                    this.os = base.Request.QueryString["os"];
                    term = term + ((term != "") ? (" and orderStatus=" + this.os) : ("orderStatus=" + this.os));
                }
                if (base.Request.QueryString["ds"] != null)
                {
                    this.ds = base.Request.QueryString["ds"];
                    term = term + ((term != "") ? (" and deliveryStatus=" + this.ds) : ("deliveryStatus=" + this.ds));
                }
                if (base.Request.QueryString["pss"] != null)
                {
                    this.pss = base.Request.QueryString["pss"];
                    term = term + ((term != "") ? (" and payStatus=" + this.pss) : ("deliveryStatus=" + this.pss));
                }
             
                DataTable sumOrderTotalAndsumDeliveryMoneyByTerm = BaseFactory.theOrder.GetsumOrderTotalAndsumDeliveryMoneyByTerm(term);
                sumCostPriceByTerm = BaseFactory.theOrder.GetSumCostPriceByTerm(term);
                num = Convert.ToDecimal(sumOrderTotalAndsumDeliveryMoneyByTerm.Rows[0]["sumOrderTotal"]);
                num2 = Convert.ToDecimal(sumOrderTotalAndsumDeliveryMoneyByTerm.Rows[0]["sumDeliveryMoney"]);
            }
            catch
            {
            }
            return ("订单总金额：" + string.Format("{0:C}", num) + "&nbsp;&nbsp;订单成本价：" + string.Format("{0:C}", sumCostPriceByTerm) + "&nbsp;&nbsp;订单运费：" + string.Format("{0:C}", num2) + "&nbsp;&nbsp;利润：" + string.Format("{0:C}", (num - sumCostPriceByTerm) - num2));
        }

        protected void Page_Load(object sender, EventArgs e)
        {
    
            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.OrderList);
                if (base.Request.QueryString["t"] != null)
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "sa", "<script type=\"text/javascript\">selectIsExitItem(\"selectOrder\",\"" + base.Request.QueryString["t"] + "\");</script>");
                }
                if (base.Request.QueryString["ps"] != null)
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "sPs", "<script type=\"text/javascript\">selectIsExitItem(\"selectPageSize\",\"" + base.Request.QueryString["ps"] + "\");</script>");
                }
                if (base.Request.QueryString["os"] != null)
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "sOs", "<script type=\"text/javascript\">selectIsExitItem(\"selectOrderStatus\",\"os=" + base.Request.QueryString["os"] + "\");</script>");
                }
                if (base.Request.QueryString["ds"] != null)
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "sDs", "<script type=\"text/javascript\">selectIsExitItem(\"selectDeliveryStatus\",\"ds=" + base.Request.QueryString["ds"] + "\");</script>");
                }
                if (base.Request.QueryString["pss"] != null)
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "sPss", "<script type=\"text/javascript\">selectIsExitItem(\"selectPayStatus\",\"pss=" + base.Request.QueryString["pss"] + "\");</script>");
                }
               
                this.OrderBind();
            }
        }

  

    }
}